//
//  SearchResultVC.m
//  ChineseCollegeStudentsNetwork
//
//  Created by Zheng on 2019/3/21.
//  Copyright © 2019 刘喆. All rights reserved.
//

#import "SearchResultVC.h"
#import "CommonModel.h"
#import "MLAWebVC.h"
#import "SearchResultCell.h"

@interface SearchResultVC () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *listArr;

@property (nonatomic, copy) NSString *last;
@property (nonatomic, copy) NSString *next;

@end

@implementation SearchResultVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSDictionary *param;
    if (_keyword) {
        param = @{@"title":_keyword,
                  @"like":@"title"};
        
        [MLAHttpManage postHttpType:MLAHttpRequestType_Get hostType:MLAHttpHostType_Api api:@"/api/v1/contents/32" params:param success:^(id  _Nonnull response, NSString * _Nonnull msg) {
            _listArr = [NSMutableArray array];
            _listArr = [CommonModel mj_objectArrayWithKeyValuesArray:response[@"value"]];
            _next = response[@"next"];
            _last = response[@"last"];
            
            if (![_next isEqualToString:_last]) {
                _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
            }
            
            [self.tableView reloadData];

        } failure:^{
            
        }];
    }
}


- (void)loadMore {
    [MLAHttpManage postHttpType:MLAHttpRequestType_Get hostType:MLAHttpHostType_Api api:_next params:nil success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        [_listArr addObjectsFromArray:[CommonModel mj_objectArrayWithKeyValuesArray:response[@"value"]]];
        _next = response[@"next"];
        _last = response[@"last"];
        
        if ([_next isEqualToString:_last]) {
            [_tableView.mj_footer endRefreshingWithNoMoreData];
        } else {
            [_tableView.mj_footer endRefreshing];
        }
        
        [self.tableView reloadData];
        
    } failure:^{
        
    }];
    
}


- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.backgroundColor = UIColor.whiteColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedSectionHeaderHeight = 0.001;
        _tableView.estimatedSectionFooterHeight = 0.001;
        [_tableView registerClass:[SearchResultCell class] forCellReuseIdentifier:@"kCellID"];
        
        UIImageView *imgv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"banner_pic"]];
        imgv.frame = CGRectMake(0, 0, 0, 125);
        _tableView.tableHeaderView = imgv;
        
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        [self.view addSubview:self.tableView];
    }
    return _tableView;
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _listArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SearchResultCell *cell = [tableView dequeueReusableCellWithIdentifier:@"kCellID" forIndexPath:indexPath];
    
    CommonModel *model = _listArr[indexPath.item];
    cell.contentLbl.text = model.title;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.0001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.0001;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CommonModel *model = _listArr[indexPath.row];
    MLAWebVC *webVC = [[MLAWebVC alloc] init];
    webVC.urlStr = model.navigationUrl;
    webVC.titleStr = _keyword;
    [self.navigationController pushViewController:webVC animated:YES];
}


@end
